/*******************************************************************************
 *    系统名称   : 约汗乐动力接口/后台管理系统
 *    客户             : 约汗乐动力
 *    文件名        ： DateUtils.java
 *              (C) Copyright 博智乐创 Corporation 2015
 *               All Rights Reserved.
 *              http://www.bozlc.com
 * *****************************************************************************
 *    注意： 本内容仅限于约汗乐动力软件公司内部使用，未经许可禁止转发
 ******************************************************************************/
package com.xiake.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

// TODO: Auto-generated Javadoc
/**
 * The Class DateUtils.
 *
 * @author 齐龙
 * 
 *         2013-9-15
 */
public class DateUtils {

    /** The format. */
    public static String format = "yyyy-MM-dd HH:mm:ss";

    /** The format_day. */
    public static String format_day = "yyyy-MM-dd";

    /** The format_ms. */
    public static String format_ms = "yyyy-MM-dd HH:mm:ss.S";

    /**  终端传输时，解析的格式，无毫秒. */
    public static String format_t = "yyyy-MM-dd'T'HH:mm:ss";

    /**  终端传输时，解析的格式，有毫秒. */
    public static String format_t_ms = "yyyy-MM-dd'T'HH:mm:ss.S";

    /**
     * Convert date4 jxl.
     *
     * @param jxlDate the jxl date
     * @return the java.util. date
     * @throws ParseException the parse exception
     */
    public static java.util.Date convertDate4JXL(java.util.Date jxlDate) throws ParseException {
        if (jxlDate == null)
            return null;
        TimeZone gmt = TimeZone.getTimeZone("GMT+16");
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        dateFormat.setTimeZone(gmt);
        String str = dateFormat.format(jxlDate);
        TimeZone local = TimeZone.getDefault();
        dateFormat.setTimeZone(local);
        return dateFormat.parse(str);
    }

    /**
     * Gets the current time.
     *
     * @return the current time
     */
    public static Date getCurrentTime() {
        return new Date();
    }

    /**
     * Parses the date from string.
     *
     * @param date the date
     * @return the date
     */
    public static Date parseDateFromString(String date) {
        return parseDateFromString(date, format);
    }

    /**
     * Parses the date from string.
     *
     * @param date the date
     * @param format the format
     * @return the date
     */
    public static Date parseDateFromString(String date, String format) {
        return parseDateFromString(date, format, java.util.Locale.US);
    }

    /**
     * Parses the date from string.
     *
     * @param date the date
     * @param format the format
     * @param locale the locale
     * @return the date
     */
    public static Date parseDateFromString(String date, String format, Locale locale) {
        SimpleDateFormat fm = new SimpleDateFormat(format, locale);
        ParsePosition pos = new ParsePosition(0);
        return fm.parse(date, pos);
    }

    /**
     * Parses the string from date.
     *
     * @param date the date
     * @return the string
     */
    public static String parseStringFromDate(Date date) {
        return parseStringFromDate(date, format);
    }

    /**
     * Parses the string from date.
     *
     * @param date the date
     * @param format the format
     * @return the string
     */
    public static String parseStringFromDate(Date date, String format) {
        return parseStringFromDate(date, format, java.util.Locale.US);
    }

    /**
     * Parses the string from date.
     *
     * @param date the date
     * @param format the format
     * @param locale the locale
     * @return the string
     */
    public static String parseStringFromDate(Date date, String format, Locale locale) {
        return parseStringFromDate(date, format, locale, null);
    }

    /**
     * Parses the string from date.
     *
     * @param date the date
     * @param format the format
     * @param locale the locale
     * @param tz the tz
     * @return the string
     */
    public static String parseStringFromDate(Date date, String format, Locale locale, TimeZone tz) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat fm = new SimpleDateFormat(format, locale);
        if (tz != null) {
            fm.setTimeZone(tz);
        }
        return fm.format(date);
    }

    /**
     * Gets the date.
     *
     * @return the date
     */
    public static Date getDate() {
        return getDate(-1, -1, -1, -1, -1, -1);
    }

    /**
     * Gets the date.
     *
     * @param year the year
     * @param month the month
     * @param dayOfMonth the day of month
     * @param hour the hour
     * @param minute the minute
     * @param second the second
     * @return the date
     */
    public static Date getDate(int year, int month, int dayOfMonth, int hour, int minute, int second) {
        return specificDate(year, month, dayOfMonth, hour, minute, second, 0, null);
    }

    /**
     * Specific date.
     *
     * @param year the year
     * @param month the month
     * @param dayOfMonth the day of month
     * @param hour the hour
     * @param minute the minute
     * @param second the second
     * @param milliSecond the milli second
     * @param zone the zone
     * @return the date
     */
    public static Date specificDate(int year, int month, int dayOfMonth, int hour, int minute, int second,
            int milliSecond, TimeZone zone) {
        Calendar c = (zone == null) ? Calendar.getInstance() : Calendar.getInstance(zone);
        if (year >= 0)
            c.set(Calendar.YEAR, year);
        if (month >= 0)
            c.set(Calendar.MONTH, month);
        if (dayOfMonth >= 0)
            c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        if (hour >= 0)
            c.set(Calendar.HOUR_OF_DAY, hour);
        if (minute >= 0)
            c.set(Calendar.MINUTE, minute);
        if (second >= 0)
            c.set(Calendar.SECOND, second);
        if (milliSecond >= 0)
            c.set(Calendar.MILLISECOND, milliSecond);
        return c.getTime();
    }

    /**
     * Adds the time for minute.
     *
     * @param date the date
     * @param value the value
     * @return the date
     */
    public static Date addTimeForMinute(Date date, int value) {
        return addTime(date, Calendar.MINUTE, value);
    }

    /**
     * Adds the time for hour.
     *
     * @param date the date
     * @param value the value
     * @return the date
     */
    public static Date addTimeForHour(Date date, int value) {
        return addTime(date, Calendar.HOUR, value);
    }

    /**
     * Adds the time for day.
     *
     * @param date the date
     * @param value the value
     * @return the date
     */
    public static Date addTimeForDay(Date date, int value) {
        return addTime(date, Calendar.DATE, value);
    }

    /**
     * Adds the time for month.
     *
     * @param date the date
     * @param value the value
     * @return the date
     */
    public static Date addTimeForMonth(Date date, int value) {
        return addTime(date, Calendar.MONTH, value);
    }

    /**
     * Adds the time for week.
     *
     * @param date the date
     * @param value the value
     * @return the date
     */
    public static Date addTimeForWeek(Date date, int value) {
        return addTime(date, Calendar.WEEK_OF_MONTH, value);
    }

    /**
     * Adds the time.
     *
     * @param date the date
     * @param field the field
     * @param value the value
     * @return the date
     */
    public static Date addTime(Date date, int field, int value) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(field, value);
        return cal.getTime();
    }

    /**
     * Sets the time.
     *
     * @param format the format
     * @param date the date
     * @param hour the hour
     * @param minute the minute
     * @param second the second
     * @return the date
     */
    public static Date setTime(String format, String date, int hour, int minute, int second) {
        if (date == null) {
            return null;
        }
        if (format == null) {
            format = format_day;
        }
        SimpleDateFormat fm = new SimpleDateFormat(format, java.util.Locale.US);
        ParsePosition pos = new ParsePosition(0);
        fm.parse(date, pos);
        Calendar cal = fm.getCalendar();
        if (hour > 0) {
            cal.set(Calendar.HOUR_OF_DAY, hour);
        }
        if (minute > 0) {
            cal.set(Calendar.MINUTE, minute);
        }
        if (second > 0) {
            cal.set(Calendar.SECOND, second);
        }
        return cal.getTime();
    }

    /**
     * 指定该date时间的小时、分钟和秒的信息。.
     *
     * @param date            指定日期时间
     * @param hour the hour
     * @param minute the minute
     * @param second the second
     * @return the date
     */
    public static Date setTime(Date date, int hour, int minute, int second) {
        /*
         * if (date == null) { return null; } Calendar cal =
         * Calendar.getInstance(); cal.setTime(date); if (hour > -1) {
         * cal.set(Calendar.HOUR_OF_DAY, hour); } if (minute > -1) {
         * cal.set(Calendar.MINUTE, minute); } if (second > -1) {
         * cal.set(Calendar.SECOND, second); } return cal.getTime();
         */
        return setTime(date, hour, minute, second, -1);
    }

    /**
     * Sets the time.
     *
     * @param date the date
     * @param hour the hour
     * @param minute the minute
     * @param second the second
     * @param millSecond the mill second
     * @return the date
     */
    public static Date setTime(Date date, int hour, int minute, int second, int millSecond) {
        if (date == null) {
            return null;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        if (hour > -1) {
            cal.set(Calendar.HOUR_OF_DAY, hour);
        }
        if (minute > -1) {
            cal.set(Calendar.MINUTE, minute);
        }
        if (second > -1) {
            cal.set(Calendar.SECOND, second);
        }
        if (millSecond > -1) {
            cal.set(Calendar.MILLISECOND, millSecond);
        }
        return cal.getTime();
    }

    /**
     * 获取日期的具体值，比如，获取日期的年、天、日、时、分、秒等信息。.
     *
     * @param date            日期
     * @param field            获取字段，Calendar.YEAR 定义的内容等。
     * @return 返回具体的数值
     */
    public static int getNumberFromDate(Date date, int field) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        return cal.get(field);
    }

    /**
     * 获取日期的‘年’。.
     *
     * @param date            日期
     * @return 返回具体的数值
     */
    public static int getYear(Date date) {
        return getNumberFromDate(date, Calendar.YEAR);
    }

    /**
     * 获取日期的‘月’。.
     *
     * @param date            日期
     * @return 返回具体的数值
     */
    public static int getMonth(Date date) {
        return getNumberFromDate(date, Calendar.MONTH) + 1;
    }

    /**
     * 获取日期的‘日’。.
     *
     * @param date            日期
     * @return 返回具体的数值
     */
    public static int getDayOfMonth(Date date) {
        return getNumberFromDate(date, Calendar.DAY_OF_MONTH);
    }

    /**
     * 将时间转换为毫秒值。.
     *
     * @param date            日期
     * @return 返回具体的数值
     */
    public static long getMilliseconds(Date date) {
        Calendar cal = Calendar.getInstance();
        if (date != null) {
            cal.setTime(date);
        }
        return cal.getTimeInMillis();
    }

    /**
     * 获取当前日期是星期几的数值。注：按照国内计数方式。周一为 1，周日为7。.
     *
     * @param date the date
     * @return the day with week
     */
    public static int getDayWithWeek(Date date) {
        int day = getNumberFromDate(date, Calendar.DAY_OF_WEEK);
        if (day == 1) {
            return 7;
        }
        return day - 1;
    }

    /**
     * 根据日期字符串判断是不是周日.
     *
     * @param dateStr the date str
     * @return true, if is week end
     */
    public static boolean isWeekEnd(String dateStr) {
        try {
            DateFormat df = new SimpleDateFormat(format_day);// 日期格式化辅助类
            Date date = df.parse(dateStr);// 格式化日期
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
            if (w < 0 || w == 0) {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * Checks if is month end.
     *
     * @param dateStr the date str
     * @return true, if is month end
     */
    public static boolean isMonthEnd(String dateStr) {
        try {
            DateFormat df = new SimpleDateFormat(format_day);// 日期格式化辅助类
            Date date = df.parse(dateStr);// 格式化日期
            Date newDate = dateAdd(date, 1); // 再过一天是一号，这天就是月末
            int day = getNumberFromDate(newDate, Calendar.DAY_OF_MONTH);
            if (day == 1) {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 判断是否是日期格式.
     *
     * @param dateStr the date str
     * @return true, if is valid date
     */
    public static boolean isValidDate(String dateStr) {
        String datePattern1 = "\\d{4}-\\d{2}-\\d{2}";
        String datePattern2 = "^((\\d{2}(([02468][048])|([13579][26]))"
                + "[\\-\\/\\s]?((((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|"
                + "(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])))))|(\\d{2}(([02468][1235679])|([13579][01345789]))[\\-\\/\\s]?("
                + "(((0?[13578])|(1[02]))[\\-\\/\\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\\-\\/\\s]?"
                + "((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\\-\\/\\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))";
        if ((dateStr != null)) {
            Pattern pattern = Pattern.compile(datePattern1);
            Matcher match = pattern.matcher(dateStr);
            if (match.matches()) {
                pattern = Pattern.compile(datePattern2);
                match = pattern.matcher(dateStr);
                return match.matches();
            } else {
                return false;
            }
        }
        return false;
    }

    /**
     * 根据指定时间获得本月的最后一天.
     *
     * @param date            指定日期
     * @return the first day of month
     */
    public static String getFirstDayOfMonth(String date) {
        String[] dateArray = date.split("-");
        int year = StringUtils.parseInt(dateArray[0]);
        int month = StringUtils.parseInt(dateArray[1]);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);// 年
        cal.set(Calendar.MONTH, month - 1);// 月，因为Calendar里的月是从0开始，所以要减1
        cal.set(Calendar.DATE, 1);// 日，设为一号
        return DateUtils.parseStringFromDate(cal.getTime(), DateUtils.format_day);// 获得月末是几号
    }

    /**
     * 根据指定时间获得本月的最后一天.
     *
     * @param date            指定日期
     * @return the last day of month
     */
    public static String getLastDayOfMonth(String date) {
        String[] dateArray = date.split("-");
        int year = StringUtils.parseInt(dateArray[0]);
        int month = StringUtils.parseInt(dateArray[1]);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);// 年
        cal.set(Calendar.MONTH, month - 1);// 月，因为Calendar里的月是从0开始，所以要减1
        cal.set(Calendar.DATE, 1);// 日，设为一号
        cal.add(Calendar.MONTH, 1);// 月份加一，得到下个月的一号
        cal.add(Calendar.DATE, -1);// 下一个月减一为本月最后一天
        return DateUtils.parseStringFromDate(cal.getTime(), DateUtils.format_day);// 获得月末是几号
    }

    /**
     * 根据指定日期，获取前几个月的今天.
     *
     * @param date            日期
     * @param num the num
     * @return the last month
     */
    public static String getLastMonth(String date, int num) {
        String[] dateArray = date.split("[- ]");
        int year = StringUtils.parseInt(dateArray[0]);
        int month = StringUtils.parseInt(dateArray[1]);
        int day = StringUtils.parseInt(dateArray[2]);
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.YEAR, year);// 年
        cal.set(Calendar.MONTH, month - 1);// 月，因为Calendar里的月是从0开始，所以要减1
        cal.set(Calendar.DATE, day);// 日，设为一号
        cal.add(Calendar.MONTH, num);// 月份加一，得到下个月的一号
        return DateUtils.parseStringFromDate(cal.getTime());// 获得月末是几号
    }

    /**
     * 判断日期是否今天之前.
     *
     * @param dateStr the date str
     * @return true, if is before today
     */
    public static boolean isBeforeToday(String dateStr) {
        try {
            DateFormat df = new SimpleDateFormat(format_day);// 日期格式化辅助类
            Date date = df.parse(dateStr);// 格式化日期
            Date now = new Date();
            // 24*60*60*1000=86400000
            if (now.getTime() - date.getTime() > 86400000) {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 时间加减.
     *
     * @param date the date
     * @param num the num
     * @return the date
     */
    public static Date dateAdd(Date date, int num) {
        try {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.add(Calendar.DATE, num);
            date = cal.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 根据年月得到某个月的天数.
     *
     * @param year            年
     * @param month            月
     * @return the sum day of month
     */
    public static int getSumDayOfMonth(int year, int month) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.set(java.util.Calendar.YEAR, year);
        c.set(java.util.Calendar.MONTH, month);
        return c.getActualMaximum

        (java.util.Calendar.DAY_OF_MONTH);
    }

    /**
     * 计算日期间隔.
     *
     * @param startDay the start day
     * @param endDay the end day
     * @return >=0, -1
     */
    public static long getDateDiff(Date startDay, Date endDay) {
        long diff = -1;
        if (endDay.getTime() - startDay.getTime() > 0) {
            diff = (endDay.getTime() - startDay.getTime()) / (24 * 60 * 60 * 1000);
        }
        return diff;

    }

    /**
     * 返回日份.
     *
     * @param date            日期
     * @return 返回日份
     */
    public static int getDay(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.DAY_OF_MONTH);
    }

    /**
     * 返回小时.
     *
     * @param date            日期
     * @return 返回小时
     */
    public static int getHour(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.HOUR_OF_DAY);
    }

    /**
     * 返回分钟.
     *
     * @param date            日期
     * @return 返回分钟
     */
    public static int getMinute(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.MINUTE);
    }

    /**
     * 返回秒钟.
     *
     * @param date            日期
     * @return 返回秒钟
     */
    public static int getSecond(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.get(java.util.Calendar.SECOND);
    }

    /**
     * 返回毫秒.
     *
     * @param date            日期
     * @return 返回毫秒
     */
    public static long getMillis(java.util.Date date) {
        java.util.Calendar c = java.util.Calendar.getInstance();
        c.setTime(date);
        return c.getTimeInMillis();
    }

    /**
     * 得到时间差。形如：1秒前，20分钟前.
     *
     * @param date1 the date1
     * @param date2 the date2
     * @return jack
     */
    public static String getTimeStr(Date date1, Date date2) {
        return getTimeStr(date1.getTime(), date2.getTime());
    }

    /**
     * Gets the time str.
     *
     * @param date1 the date1
     * @param date2 the date2
     * @return the time str
     */
    public static String getTimeStr(long date1, long date2) {
        String res = "";
        long t1 = date1;
        long t2 = date2;
        long ex = (t2 - t1) / 1000;// 秒级别
        if (ex < 60) {// 秒
            if (ex <= 0) {
                ex = 1;
            }
            res = ex + "秒前";
        } else if (ex < 60 * 60) {// 分
            res = ex / 60 + "分钟前";
        } else if (ex < 60 * 60 * 24) {// 时
            res = ex / (60 * 60) + "小时前";
        } else if (ex < (60 * 60 * 24 * 7)) {// 天
            res = ex / (60 * 60 * 24) + "天前";
        } else if (ex < (60 * 60 * 24 * 30)) { // 周
            res = ex / (60 * 60 * 24 * 7) + "周前";
        } else if (ex < (60 * 60 * 24 * 30 * 12)) { // 月
            res = ex / (60 * 60 * 24 * 30) + "月前";
        } else {
            res = parseStringFromDate(new Date(date1), "yyyy-MM-dd");
        }
        return res;
    }

    /**
     * The main method.
     *
     * @param args the arguments
     */
    public static void main(String args[]) {
        System.out.println(parseStringFromDate(new Date(), "yyyy-MM-dd"));
    }

    /** The Constant H_M. */
    public static final int H_M = 0;

    /** The Constant H_M_S. */
    public static final int H_M_S = 1;

    /** The Constant Y_H_M. */
    public static final int Y_H_M = 2;

    /** The Constant Y_H_M_H_M. */
    public static final int Y_H_M_H_M = 3;

    /** The Constant Y_H_M_H_M_S. */
    public static final int Y_H_M_H_M_S = 4;

    /** The Constant Y. */
    public static final int Y = 5;

    /** The Constant Y_H. */
    public static final int Y_H = 6;

    /** The Constant Y_H_M_H_M_S_2. */
    public static final int Y_H_M_H_M_S_2 = 7;

    /** The Constant yyyy_MM_ddTHH_mm_ss_z. */
    public static final int yyyy_MM_ddTHH_mm_ss_z = 8;

    /** The Constant YMD_HM. */
    public static final int YMD_HM = 9;

    /** The Constant MD. */
    public static final int MD = 10;

    /** The Constant yyyyMMdd. */
    public static final int yyyyMMdd = 11;

    /** The Constant Y_H_M_H. */
    public static final int Y_H_M_H = 12;

    /**
     * 根据不同的类型得到不同的格式化工具类.
     *
     * @param num            表示类型
     * @return 不同类型的格式化工具类
     */
    private static SimpleDateFormat getFormat(int num) {
        SimpleDateFormat dateformat = null;
        switch (num) {
        case DateUtils.H_M:
            dateformat = new SimpleDateFormat("HH:mm");
            break;
        case DateUtils.H_M_S:
            dateformat = new SimpleDateFormat("HH:mm:ss");
            break;
        case DateUtils.Y_H_M:
            dateformat = new SimpleDateFormat("yyyy-MM-dd");
            break;
        case DateUtils.Y_H_M_H_M:
            dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            break;
        case DateUtils.Y_H_M_H_M_S:
            dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            break;
        case DateUtils.Y:
            dateformat = new SimpleDateFormat("yyyy年");
            break;
        case DateUtils.Y_H:
            dateformat = new SimpleDateFormat("yyyy年MM月");
            break;
        case DateUtils.Y_H_M_H_M_S_2:
            dateformat = new SimpleDateFormat("yyyyMMddHHmmss");
            break;
        case DateUtils.yyyy_MM_ddTHH_mm_ss_z:
            dateformat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'+08:00'");
            break;
        case DateUtils.YMD_HM:
            dateformat = new SimpleDateFormat("yyyy.MM.dd HH:mm");
            break;
        case DateUtils.MD:
            dateformat = new SimpleDateFormat("MM.dd");
            break;
        case DateUtils.Y_H_M_H:
            dateformat = new SimpleDateFormat("yyyy-MM-dd HH");
            break;
        default:
            break;
        }
        return dateformat;
    }

    /**
     * Gets the format date.
     *
     * @param num the num
     * @param date the date
     * @return the format date
     */
    public static Date getFormatDate(int num, Date date) {
        Date resultDate = null;
        if (date == null) {
            return null;
        }
        SimpleDateFormat dateformat = getFormat(num);
        try {
            if (dateformat != null) {
                String stringDate = dateformat.format(date);
                resultDate = dateformat.parse(stringDate);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return resultDate;
    }

    // 判断A时间点 是否在B与C时间点之间 A B C
    // 返回：
    // 1：在B之前，2：在B与C中间 ， 3，在C之后 0: 错误
    /**
     * Checks if is inside two time.
     *
     * @param A the a
     * @param B the b
     * @param C the c
     * @return the int
     */
    public static int isInsideTwoTime(Date A, Date B, Date C) {
        int result = 0;
        A = getFormatDate(2, A);
        B = getFormatDate(2, B);
        C = getFormatDate(2, C);
        long a = A.getTime();
        long b = B.getTime();
        long c = C.getTime();
        if (b >= c) {
            return 0;
        } else if (a >= b && a <= c) {
            result = 2;
        } else if (a < b) {
            result = 1;
        } else if (a > c) {
            result = 3;
        }
        return result;
    }

    /**
     * Date to weekly.
     *
     * @param date the date
     * @return the string
     */
    public static String dateToWeekly(Date date) {
        String[] weekDays = { "周日", "周一", "周二", "周三", "周四", "周五", "周六" };
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }

    /**
     * Minus date.
     *
     * @param bigDate the big date
     * @param smallDate the small date
     * @return the long
     */
    public static long minusDate(Date bigDate, Date smallDate) {
        long day = 0;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String bdateStr = sdf.format(bigDate);
        String sdateStr = sdf.format(smallDate);
        try {
            bigDate = sdf.parse(bdateStr);
            smallDate = sdf.parse(sdateStr);
            day = (bigDate.getTime() - smallDate.getTime()) / (24 * 60 * 60 * 1000);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return day;
    }
    
    /**
     * String to date.
     *
     * @param format the format
     * @param sdate the sdate
     * @return the date
     */
    public static Date StringToDate(String format, String sdate) {
    	SimpleDateFormat sdf = new SimpleDateFormat(format);
    	Date date = null;
		try {
			date = sdf.parse(sdate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return date;
    }
}
